/******************************************************************
 * Created by: GOh CHee Hong                                      
 * Date of Creation: 1/Jan/2009            
 
 *                                                                
 * Description: This class is in charge of tracking a peer
 ******************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;
namespace Pbdms.BusinessLogic.PeerMS
{
    public class PeerTrackingManager
    {
        private Hashtable hPeerTrackers = new Hashtable();
        private Network.NetworkController networkctlr;
        public event EventHandler addNewTracker;
        public event EventHandler deleteNewTracker;
        public event EventHandler pollingStarts;
        public event EventHandler pollingFailed;   
        public event EventHandler pollingSuccess;
        public Hashtable HPeerTrackers
        {
            get
            {
                return hPeerTrackers;
            }
        }
        public PeerTrackingManager()
        {

        }

        public PeerTrackingManager(Network.NetworkController networkctlr)
        {
            this.networkctlr = networkctlr;
        }
        public void addATracker(string keyID, Boolean trusted)
        {
            PeerTracker peerTracker= new PeerTracker(keyID,trusted,networkctlr);
            setupEvent(peerTracker);
            hPeerTrackers.Add(keyID, peerTracker);
            if (addNewTracker != null)
            {
                addNewTracker(peerTracker, EventArgs.Empty);
            }
            Thread start = new Thread(new ThreadStart(peerTracker.pollManager.polling));
            start.IsBackground = true;
            start.Start();
            //peerTracker.authManager.authenticate();
        }
        public void deleteATracker(string keyID)
        {
            PeerTracker peerTracker= (PeerTracker )hPeerTrackers[keyID];
            unSetupEvent(peerTracker);
            hPeerTrackers.Remove(keyID);
            
            if (deleteNewTracker != null)
            {
                deleteNewTracker(peerTracker, EventArgs.Empty);
            }
        }
        private void setupEvent(PeerTracker peer)
        {

            peer.pollManager.pollingStarts += new EventHandler(pollingStarts);
            peer.pollManager.pollingFailed += new EventHandler(pollingFailed);
            peer.pollManager.pollingSuccess += new EventHandler(pollingSuccess);
        }
        private void unSetupEvent(PeerTracker peer)
        {
            peer.pollManager.pollingStarts -= new EventHandler(pollingStarts);
            peer.pollManager.pollingFailed -= new EventHandler(pollingFailed);
            peer.pollManager.pollingSuccess -= new EventHandler(pollingSuccess);
         
        }
        public void authenticateAll()
        {
            foreach (PeerTracker peer in hPeerTrackers.Values)
            {
                peer.pollManager.polling();
            }
        }
       
    }
}
